<?php
/**
 * 订单管理
 * @author a
 *
 */
class Order extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->t = $this->ct_template;
        
        $this->_nursery_id = 1;     //重置园所ID为1
        
        $this->load->model('order_model');  
        $this->load->model('member_model');  
        $this->load->model('course_cate_model');  
        $this->load->model('course_list_model');  
        $this->load->model('course_package_model');
        $this->load->model('nursery_model');
        
        //分页
        $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
        $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
        $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    /**
     * 订单列表
     */
    public function Index(){
        try {
            $order_list = $this->order_model->findAll(array(),'*','id desc',$this->offse,$this->page_size);
            if(empty($order_list))throw new Exception('没有课包记录');
            
            $total = $this->order_model->findCount(array()); //总数
            $pagelink = $this->pagination($total,$this->page_size);
            
            //获取园所名称
            $where = "id IN (". filterValue($order_list,'nursery_id',true) .")";
            $nursery_list = $this->nursery_model->findAll($where,'id,title as nursery_title');
            $nursery_list = restByKey($nursery_list,'id');
            $order_list = arrayMerge($order_list,'nursery_id',$nursery_list);	//合并
            
            //获取用户名
            $where = "id IN (". filterValue($order_list,'member_id',true) .")";
            $member_list = $this->member_model->findAll($where,'id,linkman,baby_name');
            $member_list = restByKey($member_list,'id');
            
            //获取课程分类
            $where = "id IN (". filterValue($order_list,'course_cate_id',true) .")";
            $course_cate_list = $this->course_cate_model->findAll($where,'id,title');
            $course_cate_list = restByKey($course_cate_list,'id');            
            
            //获取课程列表
            $where = "id IN (". filterValue($order_list,'course_list_id',true) .")";
            $course_list = $this->course_list_model->findAll($where,'id,title');
            $course_list = restByKey($course_list,'id');
            
            //获取课包信息
            $where = "id IN (". filterValue($order_list,'course_package_id',true) .")";
            $course_package_list = $this->course_package_model->findAll($where,'id,title');
            $course_package_list = restByKey($course_package_list,'id');
            
            
            //echo '<pre>';print_r($order_list);die();
            
        }catch (Exception $e){
            $error = $e->getMessage();
        }
        $this->t->write_view('content', 'admin/order/index', array(
            'pagelink'     =>  $pagelink,
            'order_list'    =>  $order_list,
            'member_list'  =>  $member_list,
            'course_cate_list' =>  $course_cate_list,
            'course_list'  =>  $course_list,
            'course_package_list'  =>  $course_package_list,
            'order_status'  => array('待支付','已支付','已退款','划课待审核','划课审核失败','划课审核成功','已过期'),
            'order_pay_way'  => array(1=>'微信支付',2=>'划课支付'),
            'error'     =>  empty($error) ? '' : $error,
        ), false);
        $this->t->render();
    }
    /**
     * 详情
     * @throws Exception
     */
    public function info(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('订单ID为空');
            $order_list = $this->order_model->find(array('id'=>$id));
            if(empty($order_list))throw new Exception('没有订单记录');
            
            //获取园所名称
            $where = "id = '{$order_list['nursery_id']}'";
            $nursery_list = $this->nursery_model->find($where,'id,title as nursery_title');
        
            //获取用户名
            $where = "id = '{$order_list['member_id']}'";
            $member_list = $this->member_model->find($where);
        
            //获取课程分类
            $where = "id = '{$order_list['course_cate_id']}'";
            $course_cate_list = $this->course_cate_model->find($where,'id,title');
        
            //获取课程列表
            $where = "id = '{$order_list['course_list_id']}'";
            $course_list = $this->course_list_model->find($where,'id,title');
        
            //获取课包信息
            $where = "id = '{$order_list['course_package_id']}'";
            $course_package_list = $this->course_package_model->find($where,'id,title');
        
        
            //echo '<pre>';print_r($order_list);die();
        
        }catch (Exception $e){
            $error = $e->getMessage();
        }
        $this->t->write_view('content', 'admin/order/info', array(
        		'nursery_list'	=>	$nursery_list,
            'order_list'    =>  $order_list,
            'member_list'  =>  $member_list,
            'course_cate_list' =>  $course_cate_list,
            'course_list'  =>  $course_list,
            'course_package_list'  =>  $course_package_list,
            'order_status'  => array('待支付','已支付','已退款','划课待审核','划课审核失败','划课审核成功','已过期'),
            'order_pay_way'  => array(1=>'微信支付',2=>'划课支付'),
            'error'     =>  empty($error) ? '' : $error,
        ), false);
        $this->t->render();
    }
    /**
     * 修改状态
     */
    public function update(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('课包ID为空');
            
            $status = intval($this->input->get('status'));
            if(empty($status) || !in_array($status,array(1,4)))throw new Exception('类型错误');
            
            $where['nursery_id'] = $this->_nursery_id;
            $where['id'] = $id;
            $where['status'] = 3;
            $order_info = $this->order_model->findAll($where,'id,status');
            if(empty($order_info))throw new Exception('没有课包记录');
            
            $bool = $this->order_model->update(array('status'=>$status),$where);
            if(empty($bool))throw new Exception('修改失败');
            
            redirect("/admin/order");
        }catch (Exception $e){
            die($e->getMessage());
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}






























